import {getLocalStorage} from "./auth.js";

const request = (data) => {
  return  fetch(`${process.env.VUE_APP_BASE_API}${data.url}?${queryParse(data.params)}`, {
    method: data.method,
    include: 'include'
  }).then(config =>{
    config.params = {
      ...config.params,
      // 这个每个接口 都需要传递，用来获取个人定制的数据
      cookie: getLocalStorage()
    }
    return config.text()
  }).then(response => {
    return JSON.parse(response)
  })
}

const queryParse = (query) => {
  let queryText = "";
  for(let key in query){
    queryText += `${key}=${query[key]}&`;
  }

  return queryText.slice(0,-1);

}

export default request
